package com.example.suyuan.service.impl;

import com.example.suyuan.dao.UserDao;
import com.example.suyuan.entity.User;
import com.example.suyuan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void register(User user) throws Exception {
        // Check if the username already exists
        User existingUser = userDao.findUserByUsername(user.getUsername());
        if (existingUser != null) {
            throw new Exception("Username already exists");
        }
        userDao.saveUser(user);
    }

    @Override
    public boolean login(User user) throws Exception {
        // Find user by username
        User existingUser = userDao.findUserByUsername(user.getUsername());
        if (existingUser != null && existingUser.getPassword().equals(user.getPassword())) {
            return true;
        }
        return false;
    }
}
